package cn.kgc.ubagnet.controller;

import cn.kgc.ubagnet.pojo.ProImg;
import cn.kgc.ubagnet.pojo.Product;
import cn.kgc.ubagnet.pojo.Protype;
import cn.kgc.ubagnet.service.product.ProductService;
import cn.kgc.ubagnet.service.protype.ProtypeService;
import cn.kgc.ubagnet.utils.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ProductController {
    @Resource
    ProductService productService;
    //这个分页固定了
    @GetMapping("/seleProduct")
    @ResponseBody
    public ResultEntity<PageInfo<Product>> seleProduct(@RequestParam(value = "typeid",required = false,defaultValue = "0") Integer typeid){
        PageInfo<Product> pageInfo = productService.selePro(typeid);
        return ResultEntity.successWithoutData(pageInfo);
    }

    //这个分页不固定
    @GetMapping("/seleProducttwo")
    @ResponseBody
    public ResultEntity<PageInfo<Product>> seleProducttwo(@RequestParam(value = "typeid",required = false,defaultValue = "0") Integer typeid,
                                                          @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                                                          @RequestParam(value = "pageSize",required = false,defaultValue = "8")Integer pageSize){
        PageInfo<Product> pageInfo = productService.selePro(typeid,pageNum,pageSize);
        return ResultEntity.successWithoutData(pageInfo);
    }



    //查询所有商品信息
    @GetMapping("/seleProducttext")
    @ResponseBody
    public ResultEntity<PageInfo<Product>> seleProducttext(@RequestParam(value = "typeid",required = false,defaultValue = "0") Integer typeid){
        PageInfo<Product> pageInfo = productService.seleByView(typeid);
        return ResultEntity.successWithoutData(pageInfo);
    }
@Resource
    ProtypeService protypeService;
//   多条件查询
@RequestMapping("/seleduotiaojian")
@ResponseBody
public ResultEntity<PageInfo<Product>> seleduotiaojian(@RequestParam(value = "typeid",required = false,defaultValue = "") Integer typeid,
                                                           @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                                           @RequestParam(value = "pageSize",required = false,defaultValue = "6") Integer pageSize,
                                                           @RequestParam(value = "desc",required = false,defaultValue = "") String desc,
                                                           @RequestParam(value = "typeid2",required = false,defaultValue ="-1") Integer typeid2,
                                                       @RequestParam(value = "colorid",required = false,defaultValue = "-1") Integer colorid,
                                                       @RequestParam(value = "sizeid",required = false,defaultValue = "-1") Integer sizeid,
                                                       @RequestParam(value = "price1",required = false,defaultValue = "-1") Integer price1,
                                                       @RequestParam(value = "price2",required = false,defaultValue = "-1") Integer price2,
                                                       @RequestParam(value = "proname",required = false,defaultValue = "-1") String proname){

    PageInfo<Product>   pageInfo = productService.selebyDuotioajian(typeid,typeid2,colorid,sizeid,(double)price1,(double)price2,pageNum,pageSize,desc,proname);
    return ResultEntity.successWithoutData(pageInfo);
}

//根据商品名称查询商品分类
    @RequestMapping("/seleProName")
    @ResponseBody
    public Map<String,Object> seleProName(String proname){
        Product product = productService.seleByProName(proname);
        Protype protype = protypeService.seleById(product.getTypeid());
        Protype protype1 = protypeService.seleById(protype.getParentid());
        List<Protype> list=new ArrayList<>();
        list.add(protype1);
        Map<String,Object> map=new HashMap<>();
        map.put("level2",list);
        return map;
    }

  //根据分类id查询商品分类
  @RequestMapping("/seleProtypeid")
  @ResponseBody
  public Map<String,Object> seleProtypeid(Integer id){
      Protype protype = protypeService.seleById(id);
      Protype protype1 = protypeService.seleById(protype.getParentid());
      List<Protype> list=new ArrayList<>();
      list.add(protype1);
      Map<String,Object> map=new HashMap<>();
      map.put("level2",list);
      return map;
  }


    @GetMapping("/pro/query/by/id")
    @ResponseBody
    public Product QueryProById(Integer id, Model model){
//        model.addAttribute("proid",id);
        Product product = productService.getProById(id);
        if(product!=null) return product;
        else return null;
    }
    //session存储接收到的proid
    @GetMapping("/pro/for/id")
    @ResponseBody
    public int forProduct(Integer proid,HttpSession session){
        session.setAttribute("proid",proid);
        return proid;
    }

    //商品详情页面查询
    @GetMapping("/proimg/query/by/id")
    @ResponseBody
    public ProImg QueryProImgById(HttpSession session){
        ProImg proimg = productService.getProImgById(Integer.parseInt(session.getAttribute("proid").toString()));
        return proimg;
    }


}
